package com.util;

import com.bean.DataStatistics;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class StatisticsUtil {

    private volatile static Map<String, DataStatistics> map = new HashMap<>();

    public static void add(DataStatistics dataStatistics, boolean isSuccess) {
        if (dataStatistics == null) {
            return;
        }
        String s = dataStatistics.getModuleName() + File.separator + dataStatistics.getInterfaceName()+File.separator+dataStatistics.getSaveName();
        DataStatistics dataStatistics1;
        synchronized (map) {
            if (map.containsKey(s)) {
                dataStatistics1 = map.get(s);
                dataStatistics1.setTimeStr(dataStatistics.getTimeStr());
                if (isSuccess) {
                    dataStatistics1.setSuccessNum(dataStatistics1.getSuccessNum() + 1);
                } else {
                    dataStatistics1.setErrorNum(dataStatistics1.getErrorNum() + 1);
                }
            } else {
                dataStatistics1 = dataStatistics;
                if (isSuccess) {
                    dataStatistics1.setSuccessNum(dataStatistics1.getSuccessNum() + 1);
                } else {
                    dataStatistics1.setErrorNum(dataStatistics1.getErrorNum() + 1);
                }
            }
            map.put(s, dataStatistics1);
        }
    }


    public synchronized static void save() {
        final String runPath = FileUtil.getRunPath() + File.separator + "db" + File.separator;
        if (runPath == null) {
            return;
        }
        File file = new File(runPath);
        if (!file.exists()) {
            file.mkdir();
        }

        log.info("缓存大小：" + map.size() + "");

        /**
         * 保存数据了
         */
        map.forEach((key, value) -> {
            log.info(key + "------" + value.getSaveName() + "------" + value.getSuccessNum());
            String s = value.getModuleName() + File.separator + value.getInterfaceName();
            String savePath = runPath + s + File.separator;
            File saveFile = new File(savePath);
            if (!saveFile.exists()) {
                saveFile.mkdirs();
            }
            savePath = savePath + value.getSaveName() + "-" + TimeUtil.currentDate() + ".txt";
            value.setTimeStr(System.currentTimeMillis());
            String json = new Gson().toJson(value);
            FileUtil.writerFile(json, savePath, true);
        });
        map.clear();
        map = new HashMap<>();
    }


    public static void RemoveOldFile() {
        final String runPath = FileUtil.getRunPath() + File.separator + "db" + File.separator;
        del(new File(runPath));
    }

    private static void del(File f) {
        File[] b = f.listFiles();//获取包含file对象对应的子目录或者文件
        for (int i = 0; i < b.length; i++) {
            if (b[i].isFile()) {//判断是否为文件
                if ((System.currentTimeMillis() / 1000 - b[i].lastModified() / 1000) > 86400 * 25) {
                    b[i].delete();//如果是就删除
                }
            } else {
                del(b[i]);//否则重新递归到方法中
            }
        }
    }
}
